package com.swfee.qinben;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.swfee.qinben.base.AppBaseActivity;
import com.swfee.qinben.dto.rsp.MessageRspDto;
import com.swfee.qinben.httpactions.MessageAction;
import com.swfee.qinben.util.CurenActivitUtils;

/**
 * 
 * @author ddr
 *
 */
public class T1Activity extends AppBaseActivity {

	private LayoutInflater inflater;
	Handler handler = new Handler();
	
	Map<String,String> msgTypeMap = new HashMap<String,String>();
	List <MessageRspDto> msgList = new ArrayList<MessageRspDto>();
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		// 在实际用的过程中，如果想要在子activity中得到context，需要把这个context = getParent();
		// 在我的项目中是这样做的，不过我现在测试，不需要父activity中的context好像也行，如果你遇到错，注意一下这方面。
		// Context context = this.getParent();

		super.onCreate(savedInstanceState);
		CurenActivitUtils.setHeaderTitle("我的信息", 0);
		inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		setContentView(R.layout.index_msg);
		
		msgTypeMap.put("URGENTMSG", "紧急消息");
		msgTypeMap.put("ADMINMSG", "管理员消息");
		msgTypeMap.put("HEALTHMSG", "健康信息");
		msgTypeMap.put("DOCTORMSG", "医嘱消息");
		msgTypeMap.put("FAMILYMSG", "家属消息");
		
		
		asynInitData();
	}
	
	public void asynInitData() {

		showProgressDialog("加载日程数据，请稍候..");
		final LinkedHashMap<String,MessageRspDto> msgsMap = new LinkedHashMap<String,MessageRspDto>();
		msgsMap.put("URGENTMSG", null);
		msgsMap.put("ADMINMSG", null);
		msgsMap.put("HEALTHMSG", null);
		msgsMap.put("DOCTORMSG", null);
		msgsMap.put("FAMILYMSG", null);
		new Thread(
				new Runnable() {
					
					@Override
					public void run() {
						MessageAction action = new MessageAction();
						List<MessageRspDto> queryList = action.getTopMessages();
						for (int i = 0; i < queryList.size(); i++) {
							MessageRspDto msgRsp = queryList.get(i);
							if(msgsMap.containsKey(msgRsp.getMessageType())){
								msgsMap.put(msgRsp.getMessageType(),msgRsp);
							}
						}
						//remove value null 
						for(String key: msgsMap.keySet()){
							MessageRspDto dto1 =  msgsMap.get(key);
							if( dto1!=null){
								msgList.add(dto1);
							}
						}
						msgsMap.clear();
					
	handler.post(new Runnable() {
							
							@Override
							public void run() {
								hideProgressDialog();
								ListView listView = (ListView) findViewById(R.id.listView);
								listView.setAdapter(new MsgAdapter());
								
								listView.setOnItemClickListener(new OnItemClickListener() {

									@Override
									public void onItemClick(AdapterView<?> arg0, View arg1, int index,
											long arg3) {
										Bundle bu = new Bundle();
										bu.putString("typeName", msgList.get(index).getMessageType());
										CurenActivitUtils.startActivity(MyMsgListActivity.class,false,bu);
									}
								});
								
							}
						});
						

					}
				}
				).start();
		
	}

	// 适配器
	private final class MsgAdapter extends BaseAdapter  {
		@Override
		public int getCount() {
			return msgList.size();
		}

		@Override
		public Object getItem(int position) {
			return msgList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			MessageRspDto model = msgList.get(position);
			if(model==null){
				return null;
			}
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.msg_include, null);
				if(position==0 && model.getMessageType().equals("URGENTMSG")){
					CurenActivitUtils.setTextViewContent(R.id.msg_type, msgTypeMap.get(model.getMessageType()), convertView,R.color.red);
				}else{
					CurenActivitUtils.setTextViewContent(R.id.msg_type, msgTypeMap.get(model.getMessageType()), convertView);
				}
				CurenActivitUtils.setTextViewContent(R.id.msg_time, model.getSubmitDateTime(), convertView);
				CurenActivitUtils.setTextViewContent(R.id.msg_content, model.getContent(),convertView);

			}
			return convertView;
		}

		

	}
}
